N = 5000000
f = [0 for i in xrange(10)]
f[0] = 1
for i in xrange(1, 10):
    f[i] = i * f[i-1]

ans = 0

for i in xrange(3,N):
    a = [int(x) for x in list(str(i))]
    s = sum([f[x] for x in a])
    if s == i:
        ans += i

print ans
